package com.eqdd.yiqidian.base;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.api.DataInjector;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.Serializable;
import java.util.List;

public abstract class BaseApplyActivity extends SlimBaseMultiEditActivity {
    private ApplayLizhiActivityCustom dataBinding;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_applay_lizhi);
        initTitle();
        initTopRightText("提交", v -> {
            if (checkParams()) {
                getPostRequest().execute(new DialogCallBack<HttpResult>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isSave = false;
                            clearData();
                            setResult(Config.REFRESH);
                            finish();
                        }
                    }
                });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    protected abstract PostRequest<HttpResult> getPostRequest();

    protected abstract void initTitle();

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    protected void setEditBeans() {
        Serializable bean = getIntent().getSerializableExtra(Config.BEAN);
        if (bean != null) {
            loadFromBean(bean);
            isSave = false;
            isRead = false;
        } else {
            loadFromLocal();
        }
    }

    protected abstract void loadFromBean(Serializable bean);
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    public void loadFromLocal() {
        if (isRead) {
            List<SlimEditBean> data = (List<SlimEditBean>) SPUtil.getParam(getLocalClassName() + user.getUname());
            if (data != null) {
                datas.clear();
                datas.addAll(data);
                DataInjector.recoverData(this);
            } else {
                loadFromInit();
            }
        }else {
            loadFromInit();
        }
    }

    public void recoverData() {
    }

    protected abstract void loadFromInit();
}
